
<template>
  <div>
    <div class="top">
      <h1>{{ $route.meta.title }}</h1>
      <el-button type="primary" @click="add">添加品类</el-button>
    </div>
    <p>当前商品的ID:0</p>
    <el-table :data="Datalist" style="width: 100%">
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="name" label="品类名称"></el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="update(scope.row)">修改名称</el-button>
          <el-button size="mini" @click="look(scope.row)"
            >查看其子品类</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { Table, TableColumn, Button } from "element-ui";
import { cates, updateClassify } from "../../../../api/index";

export default {
  name: "cates",
  components: {
    [Table.name]: Table,
    [TableColumn.name]: TableColumn,
    [Button.name]: Button,
  },
  data() {
    return {
      Datalist: [],
    };
  },
  mounted() {
    cates({ categoryId: 0 }).then((res) => {
      // console.log(res);
      this.Datalist = res.data.data;
    });
  },
  methods: {
    update(data) {
      let name = prompt("请输入修改后的数据", data.name);
      if (name != null) {
        updateClassify({ categoryId: data.id, categoryName: name }).then(
          (res) => {
            if (res.data.status == 0) {
              alert(res.data.data);
              //重新加载
              cates({ categoryId: 0 }).then((res) => {
                // console.log(res);
                this.Datalist = res.data.data;
              });
            }
          }
        );
      }
    },
    look(data) {
      this.$router.push("/product/index/" + data.id);
    },
    add() {
      this.$router.push("/product/index/cate/add");
    },
  },
};
</script>

<style lang="scss" scoped>

.top {
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
  align-items: center;
}
h1 {
  font-weight: normal;
}
</style>